package com.ray.exercise.security.realms;

import com.ray.exercise.utils.DigestsUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author reny
 * created at 2024/6/5 19:43
 **/
public class UserRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken loginToken = (UsernamePasswordToken) token;
        //假定数据库的密码为“123”的md5值，盐值是username的值
        String password = "123";
        String passwordHash = DigestsUtils.md5(password, loginToken.getUsername());
        return new SimpleAuthenticationInfo(loginToken.getUsername(), passwordHash, getName());
    }
}
